# -*- coding: utf-8 -*-
# @File     : dorequest.py
# @Author   : hewenjun
# @Desc     : 接口请求类，封装http的get、post方法以及dubbo接口的请求方法

import dubbo_telnet
import yaml
import requests


class MyRequest(object):
    headers = {'Content-Type': 'application/json'}
    verify = False
    url, param, json, host, port, interface, method, api_name, name, title, step_name, pre = [], [], [], [], [], [], [], [], [], [], [], []

    def __init__(self, filename):
        self.url, self.param, self.json, self.host, self.port, self.interface, self.method, self.api_name, self.name, self.title, self.step_name, self.pre = [], [], [], [], [], [], [], [], [], [], [], []
        requests.packages.urllib3.disable_warnings()
        datas = self.buildRequest(filename)
        for data in datas:
            if data['type'] == 'http':
                self.url.append(data.get('baseurl') + data.get('api'))
                self.param.append(data.get('param'))
                self.json.append(data.get('data'))
            if data['type'] == 'dubbo':
                self.param.append(data.get('param'))
                self.host.append(data.get('host'))
                self.port.append(data.get('port'))
                self.interface.append(data.get('interface'))
                self.method.append(data.get('method'))
            self.api_name.append(data.get('api_name'))
            self.name.append(data.get('name'))
            self.title.append(data.get('title'))
            self.step_name.append(data.get('step_name'))
            self.pre.append(data.get('pre'))
        # print(self.url[0], self.param[0], self.json[0])

    def get(self, index):
        '''封装get方法'''
        requests.packages.urllib3.disable_warnings()
        try:
            res = requests.get(url=self.url[index], params=self.param[index], headers=self.headers,
                               verify=self.verify).json()
            return res
        except Exception as e:
            print("get请求错误: %s" % e)

    def post(self, index):
        '''封装post方法'''
        requests.packages.urllib3.disable_warnings()
        try:
            res = requests.post(url=self.url[index], params=self.param[index], json=self.json[index],
                                headers=self.headers, verify=self.verify).json()
            # self.log.info("响应的内容：%s" % res.json())
            # self.log.info("返回的状态码：%s" % res.status_code)
            return res
        except Exception as e:
            print("post请求错误: %s" % e)

    def delete(self, url, **kwargs):
        requests.packages.urllib3.disable_warnings()
        '''封装delete方法'''
        params = kwargs.get("params")
        data = kwargs.get("data")
        json = kwargs.get("json")
        files = kwargs.get("files")
        try:
            res = requests.delete(url, params=params, data=data, json=json, files=files)
            self.log.info("响应的内容：%s" % res.json())
            self.log.info("返回的状态码：%s" % res.status_code)
            return res
        except Exception as e:
            print("post请求错误: %s" % e)

    def put(self, url, **kwargs):
        '''封装put方法'''
        requests.packages.urllib3.disable_warnings()
        params = kwargs.get("params")
        data = kwargs.get("data")
        json = kwargs.get("json")
        files = kwargs.get("files")
        try:
            res = requests.put(url, params=params, data=data, json=json, files=files)
            self.log.info("响应的内容：%s" % res.json())
            self.log.info("返回的状态码：%s" % res.status_code)
            return res
        except Exception as e:
            print("post请求错误: %s" % e)

    def dubbo(self, index):
        """
        :param interface: 接口地址
        :param method: 请求方式
        :param param: 参数
        :param host: ip
        :param port: 端口
        :return: 响应
       """
        try:
            conn = dubbo_telnet.connect(self.host[index], self.port[index])
            conn.set_connect_timeout(10)
            conn.set_encoding("utf-8")
            command = conn.invoke(self.interface[index], self.method[index], self.param[index])
            return command
        except Exception as e:
            print("dubbo接口请求错误::::{}".format(e))

    def buildRequest(self, filename):
        with open(filename, encoding='utf-8') as yamlfile:
            data = yaml.load(yamlfile, Loader=yaml.FullLoader)
        return data
